//
//  ReportFormsViewController.m
//  MyProject
//
//  Created by Pei on 16/6/29.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

/*
 
 {
 InspectType = "";
 ItemName = "";
 OrgList =             (
 {
 AnalysisCount = 0;
 OrgCode = CWB;
 OrgName = "\U4e09\U516c\U53f8";
 },
 {
 AnalysisCount = 0;
 OrgCode = "A_100043";
 OrgName = "\U6d66\U4e1c\U5206\U516c\U53f8";
 }
 );
 ProjectList = "";
 RiskLevel = "";
 RiskType = 5;
 },
 
 */

#import "ReportFormsViewController.h"
#import "UUChart.h"

@interface ReportFormsViewController ()<UUChartDataSource>
{
    UUChart *_chartView;
    
    NSArray *dataArray;
    
    NSArray *XXXArray;
    NSArray *YYYArray;
    
    NSArray *YYYValueArray;
    
    int maxInt;
    
    NSMutableArray *colorsArray;
    
    int typeInt;
}
@end

@implementation ReportFormsViewController

- (id)initWithData:(NSArray*)data typeInt:(int)type level:(int)levelInt{
    self = [super init];
    if (self) {
        
        colorsArray = [[NSMutableArray alloc] initWithCapacity:3];
        [colorsArray removeAllObjects];
        
        maxInt = 0;
        
        dataArray = data;
        
        typeInt = type;
        
        XXXArray = dataArray;
        NSMutableArray *tempAry = [[NSMutableArray alloc] initWithCapacity:3];
        [tempAry removeAllObjects];
        
        for (NSDictionary *tempD in dataArray) {
            NSArray *OrgList;// = [tempD objectForKey:@"OrgList"];
            if (levelInt == 0) {
              OrgList = [tempD objectForKey:@"OrgList"];
            }else{
               OrgList = [tempD objectForKey:@"ProjectList"];
            }
            
            [tempAry addObject:OrgList];
        }
        YYYArray = tempAry;
        
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(16, 16, 22, 22)];
        [backButton setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateNormal];
        [backButton setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateHighlighted];
        [backButton addTarget:self action:@selector(navigationBack) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:backButton];
        
        if (YYYArray == nil || YYYArray.count <= 0) {
            return self;
        }
        
        for (int x = 0;x < YYYArray.count; x ++) {
            if (x == 0) {
                NSArray *ggg = [YYYArray objectAtIndex:0];
                for (int z = 0; z < ggg.count; z ++) {
                    [colorsArray addObject:[self randomColor]];
                }
            }
        }
        
        for (int x = 0;x < YYYArray.count; x ++) {
            NSArray *NNNNN = [YYYArray objectAtIndex:0];
            if (!(NNNNN != nil && NNNNN.count > 0)) {
                return self;
            }
            if (x == 0) {
                NSArray *ggg = [YYYArray objectAtIndex:0];
                if (ggg.count > 1) {
                    for (int z = 0; z < ggg.count; z ++) {
                        //[colorsArray addObject:[self randomColor]];
                        
                        NSDictionary *nameA = [ggg objectAtIndex:z];
                        
                        UIView *sss = [[UIView alloc] init];
                        sss.frame = CGRectMake(kDeviceHeight-120, 20.0f+10*z+2*z, 10.0f, 10.0f);
                        sss.backgroundColor = [colorsArray objectAtIndex:z];
                        [self.view addSubview:sss];
                        
                        UILabel *titleLbl = [[UILabel alloc] init];
                        titleLbl.frame = CGRectMake(sss.right+2.0f, sss.origin.y, 100, 10.0f);
                        titleLbl.font = kAppFont(10.0f);
                        if (levelInt == 0) {
                            titleLbl.text = [nameA valueForKey:@"OrgName"];
                        }else{
                            
                            titleLbl.text = [nameA valueForKey:@"ProjectName"];
                        }
                        
                        [self.view addSubview:titleLbl];
                    }
                }else{
                    NSDictionary *nameA = [ggg objectAtIndex:0];
                    
                    UIView *sss = [[UIView alloc] init];
                    sss.frame = CGRectMake(kDeviceHeight-120, 20.0f, 10.0f, 10.0f);
                    sss.backgroundColor = kRGB(154, 233, 171);
                    [self.view addSubview:sss];
                    
                    UILabel *titleLbl = [[UILabel alloc] init];
                    titleLbl.frame = CGRectMake(sss.right+2.0f, sss.origin.y, 100, 10.0f);
                    titleLbl.font = kAppFont(10.0f);
                    if (levelInt == 0) {
                        titleLbl.text = [nameA valueForKey:@"OrgName"];
                    }else{
                        
                        titleLbl.text = [nameA valueForKey:@"ProjectName"];
                    }

                    //titleLbl.text = [nameA valueForKey:@"OrgName"];
                    [self.view addSubview:titleLbl];
                }

            }
        }
        
        
        
        NSMutableArray *arrayYY = [[NSMutableArray alloc] initWithCapacity:3];
        [arrayYY removeAllObjects];
        
        for (NSArray *dddd in YYYArray) {
            NSMutableArray *stringArray = [[NSMutableArray alloc] initWithCapacity:3];
            [stringArray removeAllObjects];
            for (NSDictionary *str in dddd) {
                
                int tempInt = [[str valueForKey:@"AnalysisCount"] intValue];
                if (tempInt > maxInt) {
                    maxInt = tempInt;
                }
                
                [stringArray addObject:[NSNumber numberWithInt:[[str valueForKey:@"AnalysisCount"] intValue]]];
            }
            [arrayYY addObject:stringArray];
        }
        YYYValueArray = arrayYY;
        
        
        
        
        
        _chartView = [[UUChart alloc] initWithFrame:CGRectMake(10, backButton.bottom+10.0f, kDeviceHeight-20, kDeviceWidth-16.0f-backButton.height-10.0f-40.0f)
                                         dataSource:self
                                              style:UUChartStyleBar colors:colorsArray];
        _chartView.backgroundColor = [UIColor clearColor];
        [_chartView showInView:self.view];

        
    }
    return self;
}

- (UIColor *) randomColor{
    CGFloat hue = ( arc4random() % 256 / 256.0 );  //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5;  // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5;  //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //kDeviceWidth-16.0f-backButton.height-10.0f-40.0f
}

- (void)navigationBack
{
    [self dismissViewControllerAnimated:YES completion:^{}];
}

- (NSArray *)getXTitles:(NSInteger)num
{
    NSMutableArray *xTitles = [NSMutableArray array];
    for (int i=0; i<num; i++) {
        NSDictionary *ddd = [dataArray objectAtIndex:i];
        if (typeInt == 0) {
            NSArray *tempT = [[NSUserDefaults standardUserDefaults] objectForKey:Dic_RiskType];
            if (tempT != nil && tempT.count > 0) {
                for (NSDictionary *fff in tempT) {
                    if ([[fff valueForKey:@"Value"] intValue] == [[ddd objectForKey:@"RiskType"] intValue]) {
                        NSString * str = [fff valueForKey:@"Name"];
                        [xTitles addObject:str];
                    }
                }
            }
        }else if (typeInt == 1){
            NSArray *tempT = [[NSUserDefaults standardUserDefaults] objectForKey:Check_Type_Items];
//            if (tempT != nil && tempT.count > 0) {
//                for (NSDictionary *fff in tempT) {
//                    if ([[fff valueForKey:@"Value"] intValue] == [[tempD objectForKey:@"InspectType"] intValue]) {
//                        nameStr = [fff valueForKey:@"Name"];
//                    }
//                }
//            }
            
            if (tempT != nil && tempT.count > 0) {
                for (NSDictionary *fff in tempT) {
                    if ([[fff valueForKey:@"Value"] intValue] == [[ddd objectForKey:@"InspectType"] intValue]) {
                        NSString * str = [fff valueForKey:@"Name"];
                        [xTitles addObject:str];
                    }
                }
            }
            
        }else if (typeInt == 2){
            //select_RiskLevel
            NSArray *tempT = [[NSUserDefaults standardUserDefaults] objectForKey:select_RiskLevel];
            if (tempT != nil && tempT.count > 0) {
                for (NSDictionary *fff in tempT) {
                    if ([[fff valueForKey:@"Value"] intValue] == [[ddd objectForKey:@"RiskLevel"] intValue]) {
                        NSString * str = [fff valueForKey:@"Name"];
                        [xTitles addObject:str];
                    }
                }
            }

        }else{
                NSString * str = [ddd valueForKey:@"ItemName"];
                [xTitles addObject:str];
        }
        
        //NSString * str = [NSString stringWithFormat:@"R-%d",i];
        //[xTitles addObject:str];
    }
    return xTitles;
}


#pragma mark - @required
//横坐标标题数组
- (NSArray *)chartConfigAxisXLabel:(UUChart *)chart
{
    return [self getXTitles:dataArray.count];
}
//数值多重数组
- (NSArray *)chartConfigAxisYValue:(UUChart *)chart
{
    NSArray *ary4 = YYYValueArray;//@[@[@"26",@"45",@"40"],@[@"42",@"26",@"40"],@[@"25"],@[@"15"],@[@"30"]];
    return @[ary4];
}

#pragma mark - @optional
//颜色数组
- (NSArray *)chartConfigColors:(UUChart *)chart
{
    return @[[UUColor green],[UUColor red],[UUColor brown]];
}
//显示数值范围
- (CGRange)chartRange:(UUChart *)chart
{
    return CGRangeMake(maxInt+20, 0);
}

#pragma mark 折线图专享功能

//标记数值区域
- (CGRange)chartHighlightRangeInLine:(UUChart *)chart
{
    return CGRangeMake(25, 75);
}

//判断显示横线条
- (BOOL)chart:(UUChart *)chart showHorizonLineAtIndex:(NSInteger)index
{
    return YES;
}

//判断显示最大最小值
- (BOOL)chart:(UUChart *)chart showMaxMinAtIndex:(NSInteger)index
{
    return YES;
}


- (BOOL)shouldAutorotate
{
    return NO;
}

-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
    return UIInterfaceOrientationLandscapeRight;
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscapeRight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
